import axios, { AxiosResponse } from "axios";
import { MenuModel } from "../models/Menu";

class MenuApis {
    queryMenu(id: string): Promise<Array<MenuModel>> {
        let promise: Promise<Array<MenuModel>> = new Promise((resolve, reject) => {
            let obj: object = {
                user: id
            };
            axios.get('/api/menu/menu', { params: obj })
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        let menuList: Array<MenuModel> = result.data.data;
                        resolve(menuList);
                    } else {
                        reject(result.data);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
    insertMenu(menu: MenuModel): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            axios.post('/api/menu/menu', menu)
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
    deleteMenu(id: number, userId: string): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let obj: object = {
                user: userId,
                id: id
            };
            axios.delete('/api/menu/menu', { params: obj })
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const menuApis: MenuApis = new MenuApis();